package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Banner;
import com.java.second_goods.service.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图信息
 */
@RestController
@RequestMapping("/api/banner")
@Tag(name = "轮播图管理", description = "轮播图相关接口")
public class BannerController extends BaseController {

    @Autowired
    private BannerService bannerService;

    /**
     * 获取轮播图列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取轮播图列表", description = "获取轮播图列表")
    public AjaxResult list(Banner banner) {
        List<Banner> list = bannerService.selectBannerList(banner);
        return success(list);
    }

    /**
     * 获取轮播图详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取轮播图详细信息", description = "根据轮播图ID获取轮播图详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(bannerService.selectBannerById(id));
    }

    /**
     * 新增轮播图
     */
    @PostMapping
    @Operation(summary = "新增轮播图", description = "新增轮播图")
    public AjaxResult add(@RequestBody Banner banner) {
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改轮播图
     */
    @PutMapping
    @Operation(summary = "修改轮播图", description = "修改轮播图")
    public AjaxResult edit(@RequestBody Banner banner) {
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除轮播图
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除轮播图", description = "删除轮播图")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(bannerService.deleteBannerByIds(ids));
    }

    /**
     * 获取有效的轮播图列表
     */
    @GetMapping("/enabled")
    @Operation(summary = "获取有效的轮播图列表", description = "获取有效的轮播图列表")
    public AjaxResult getEnabledBanners() {
        List<Banner> banners = bannerService.selectEnabledBanners();
        return success(banners);
    }

    /**
     * 根据类型查询轮播图列表
     */
    @GetMapping("/type/{type}")
    @Operation(summary = "根据类型查询轮播图列表", description = "根据类型查询轮播图列表")
    public AjaxResult getBannersByType(@PathVariable("type") String type) {
        List<Banner> banners = bannerService.selectBannersByType(type);
        return success(banners);
    }

    /**
     * 更新轮播图状态
     */
    @PutMapping("/status/{bannerId}/{status}")
    @Operation(summary = "更新轮播图状态", description = "更新轮播图状态")
    public AjaxResult updateStatus(@PathVariable("bannerId") Integer bannerId,
                                   @PathVariable("status") Integer status) {
        return toAjax(bannerService.updateBannerStatus(bannerId, status));
    }
}